package com.strong.faker;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.RandomUtil;
import com.strong.enum_.ProvinceEnum;
import lombok.extern.slf4j.Slf4j;

import java.util.Arrays;

/**
 * 固定电话 模拟数据生成类
 *
 * @author simen
 * @date 2023/04/07
 */
@Slf4j
public class TelephoneFaker extends BaseFaker<String> {

    /**
     * 省枚举数组
     */
    private ProvinceEnum[] provinceEnums;

    /**
     * 区号代码数组
     */
    private String[] strsAreaCode;

    /**
     * 是否初始化
     */
    private boolean isInitialized = false;

    /**
     * 注入省枚举
     *
     * @param provinceEnums 省枚举
     * @return {@link PlaceFaker}
     */
    public TelephoneFaker province(ProvinceEnum... provinceEnums) {
        this.provinceEnums = provinceEnums;
        this.isInitialized = false;
        return this;
    }

    @Override
    protected void initialize() {
        // 如果枚举数组为空，则默认所有枚举
        if (ArrayUtil.isEmpty(provinceEnums)) {
            provinceEnums = ProvinceEnum.values();
        }

        // 如果区号数组为空，则初始化数组
        if (!isInitialized || ArrayUtil.isEmpty(strsAreaCode)) {
            this.strsAreaCode = ArrayUtil.addAll(Arrays.stream(provinceEnums).map(ProvinceEnum::getAreaCodes).toArray(String[][]::new));
            this.isInitialized = true;
        }

        log.info("【TelephoneFaker】区号参数：{}", ArrayUtil.toString(this.strsAreaCode));
    }

    @Override
    protected String getModel() {
        String strAreaCode = RandomUtil.randomEle(this.strsAreaCode);
        return "%s-%s".formatted(strAreaCode, RandomUtil.randomInt(1000000, 99999999));
    }
}
